'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 科技
 * @author sundbcn@gmail.com (sun debing)
 */
var _ = require('lodash');
var dataApi = require('../libs/dataApi');

/**
 * 通过科技ID和等级 获取加成值value
 * @param  {[type]} tech_id [description]
 * @param  {[type]} tech_lv [description]
 * @return {[type]}     [description]
 */
exports.getTechEffValue = function (tech_id, tech_lv) {
    var tech_lvl_conf = _getTechLvlConf(tech_id, tech_lv);
    if (!tech_lvl_conf) {
        console.error('tech_lvl_conf type can not found:' + tech_id);
        return 0;
    }
    return tech_lvl_conf.value;
};

/**
 * 获取科技开启关系配置
 * @param  {[type]} tech_id [description]
 * @return {[type]}     [description]
 */
exports.getTechOpenConf = function (tech_id) {
    return dataApi.tech_open.findById(tech_id);
};

/**
 * 获取科技等级配置
 * @param  {[type]} tech_id [description]
 * @param  {[type]} tech_lv [description]
 * @return {[type]}     [description]
 */
exports.getTechLvlConf = function (tech_id, tech_lv) {
    return _getTechLvlConf(tech_id, tech_lv);
};

// 获取科技ID列表
exports.getTechIdList = function (tech_class) {
    return _getTechIdList(tech_class);
};

///////////////////////////////////////////////////////////////////////////////////
/**
 * 获取科技等级配置
 * @param  {[type]} tech_id [description]
 * @param  {[type]} tech_lv [description]
 * @return {[type]}     [description]
 */
var _getTechLvlConf = function (tech_id, tech_lv) {
    var tech_lvl_info = dataApi.tech_lvl.data;
    if (!tech_lvl_info[tech_id]) {
        console.error('tech_lvl_info type can not found:' + tech_id);
        return null;
    }
    return _.find(tech_lvl_info[tech_id], {
        lvl: tech_lv
    });
};

/**
 * 获取科技类型配置
 * @param  {[type]} tech_type [description]
 * @return {[type]}     [description]
 */
var _getTechTypeConf = function (tech_type) {
    return dataApi.tech_type.findById(tech_type);
};

/**
 * 获取科技ID列表
 * @param  {[type]} type [description]
 * @return {[type]}     [description]
 */
var _getTechIdList = function (tech_class) {
    var tech_id_list = [];
    var tech_lvl_info = dataApi.tech_lvl.data;
    for (var tech_id in tech_lvl_info) {
        if (typeof (tech_lvl_info[tech_id]) === 'function') {
            continue;
        }

        var tech_lvl_cnf = tech_lvl_info[tech_id];
        if (!!tech_lvl_cnf && tech_lvl_cnf[0].type === tech_class) {
            tech_id_list.push(parseInt(tech_id));
        }
    }
    return tech_id_list;
};

/**
 * 获取科技等级配置
 * @param  {[type]} tech_id [description]
 * @param  {[type]} tech_lv [description]
 * @return {[type]}     [description]
 */
exports.testGet = function (tech_id, tech_lv) {
    var tech_info = dataApi.tech_lvl.findById(tech_id);
    if (!tech_info) {
        console.error('tech_info type can not found:' + tech_id);
        return null;
    }
    return _.find(tech_info, {
        lvl: tech_lv
    });
};

/**
 * 获取科技常量配置
 * @return {[type]}     [description]
 */
exports.getBuildConstVal = function () {
    var tech_const_list = [];
    var tech_const_json = dataApi.tech_build.data;
    for (var const_id in tech_const_json) {
        if (typeof (tech_const_json[const_id]) === 'function') {
            continue;
        }

        var tech_const_info = tech_const_json[const_id];
        tech_const_list.push({
            id: tech_const_info.id,
            value: tech_const_info.value
        });
    }
    return tech_const_list;
};
